//
//  SocketEngineWebSocket.m
//  Socket.IO-Client
//
//  Created by alanwang.dev on 16/8/8.
//  Copyright © 2016年 com.alanwang.dev. All rights reserved.
//

#import "SocketEngineWebSocket.h"
#import "SocketTypes.h"

@implementation SocketEngineWebSocket

- (void)probeWebSocket{
    if (self.ws.isConnected) {
        [self sendWebSocketMessage:@"probe" type:PacketType_Ping datas:nil];
    }
}

/// Send message on WebSockets
/// Only call on emitQueue
- (void)sendWebSocketMessage:(NSString *)str type:(SocketEnginePacketType)type datas:(NSMutableArray<NSData *> *)datas{
    // TODO: DefaultSocketLogger
    
    // TODO: 此处的rawValue 和hashValue 可能需要调整
    [self.ws wirteString:[NSString stringWithFormat:@"%d%@",type,str] completion:nil];
    
    for (NSData *data in datas) {
            // TODO: 此处的传值类型存在问题
        Either *either = [self createBinaryDataForSend:data];
        if (either.Left && [either.Left isKindOfClass:[NSMutableData class]]) {
            [self.ws writeData:either.Left completion:nil];
        }
    }
}

- (void)websocketDidReceiveMessage:(WebSocket *)socket text:(NSString *)text{
    [self parseEngineMessage:text fromPolling:NO];
}

- (void)websocketDidReceiveData:(WebSocket *)socket data:(NSData *)data{
    [self parseEngineData:data];
}

@end
